package com.chief.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.KafkaSourceBuilder;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;

public class SourceTest {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //从socket读取
        DataStreamSource<String> s1 = env.socketTextStream("hadoop52", 7777);

        // 从文件读取
        DataStreamSource<String> s2 = env.readTextFile("");

        //从集合数据读取
        DataStreamSource<String> s3 = env.fromCollection(Arrays.asList("", ""));

        // 从kafka读取
        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers("hadoop53:9092,hadoop54:9092,hadoop55:9092")
                .setTopics("inTest")
                .setStartingOffsets(OffsetsInitializer.earliest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        DataStream<String> kafka = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source");

        kafka.print();

        env.execute("kafka tests");
    }
}
